const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const ExtractPlugin = require('extract-text-webpack-plugin')
const HTMLPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const isDev = process.env.NODE_ENV === 'development'

function resolve(track) {
	return path.join(__dirname, track)
}
const config = {
	entry: path.join(__dirname, './app.js'), 
	output: {
		filename: '[name].[hash:8].js',
		path: path.join(__dirname, 'dist'),
		publicPath: './'
	},
	plugins: [
		new HTMLPlugin(),
		new VueLoaderPlugin(),
		new webpack.DefinePlugin({
			'process.env': {
				NODE_ENV: isDev ? '"development"' : '"production"'
			}
		})
    ],
    resolve: {
		extensions: ['.js', '.vue', '.json'],
		alias: {
            vue$: 'vue/dist/vue.esm.js',
            'jquery': 'jquery/src/jquery.js',
			'@': resolve('src/'),
			'@images': resolve('src/images/'),
			'@lib': resolve('src/lib/'),
			'@api': resolve('src/api/'),
			'@pages': resolve('src/pages/'),
			'@utils': resolve('src/utils/'),
			'@style': resolve('src/style/'),
			'@store': resolve('src/store/'),
		}
	},
	module: {
		rules: [
			{
				test: /\.vue$/,
				loader: 'vue-loader' // 处理以.vue结尾的文件
			},
			{ test: /\.css$/, use: ['vue-style-loader', 'css-loader'] },
			{
				test: /\.scss$/,
				loader: 'vue-style-loader!css-loader!sass-loader'
			},
			{
				test: /\.less$/,
				loader: 'style-loader!css-loader!less-loader'
			},
			//使用babel-loader来处理src下面的所有js文件,具体babel配置在.babelrc,主要是用来转义es6
			{
				test: /\.js$/,
				use: {
					loader: 'babel-loader',
					options: {}
				},
				include: resolve('src')
			},
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: 'img/[name].[hash:7].[ext]'
                }
            },
            {
                test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: 'media/[name].[hash:7].[ext]'
                }
            },
            {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: 'fonts/[name].[hash:7].[ext]'
                }
            }
		]
	}
}
module.exports = config
